<template>
  <el-dialog v-model="dialogVisible" title="退款审核">
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="120px">
      <el-form-item label="订单编号:">
        <span>{{ content.orderNumber }}</span>
      </el-form-item>
      <el-form-item label="订单创建时间:">
        <span>{{
          formatDate(new Date(content.createdDate), "YYYY-mm-dd HH:MM:SS")
        }}</span>
      </el-form-item>
      <el-form-item label="订单金额:">
        <span>{{ content.transactionAmount }}元</span>
      </el-form-item>
      <el-form-item label="预订人:">
        <span>{{ content.orderUserName }} - {{ content.phoneNumber }}</span>
      </el-form-item>
      <el-form-item label="退款原因:">
        <span>{{ content.refundFailureReason }}</span>
      </el-form-item>
      <el-form-item label="退款进度" prop="refundStatus">
        <el-select v-model="ruleForm.refundStatus" placeholder="请选择退款进度">
          <el-option v-for="item in state.optionStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="不通过原因" prop="refundFailureReason">
        <el-input v-model="ruleForm.refundFailureReason" type="textarea" placeholder="请输入不通过原因" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submitRules(ruleFormRef)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { formatDate } from "@/utils/formatTime";
import { RefundStatusEnum } from "@/views/area/config/enum";
import { postOrderRefund } from "@/api/area/index";

const props = defineProps(["content"]);
const dialogVisible = ref(false);
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  refundFailureReason: "",
  refundStatus: "",
  orderVenueId: ""
});
const state = reactive({
  optionStatus: [
    {
      label: "通过",
      value: RefundStatusEnum.AUDIT_PASS,
    },
    {
      label: "不通过",
      value: RefundStatusEnum.AUDIT_NO_PASS,
    },
    {
      label: "审核中",
      value: RefundStatusEnum.AUDITING,
    },
    {
      label: "已完成",
      value: RefundStatusEnum.COMPLETED,
    },
  ],
});

watch(() => props.content.refundStatus, (newVal, oldVal) => {
  ruleForm.refundStatus = newVal;
})

// 提交退款审核按钮
const submitRules = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      ruleForm.orderVenueId = props.content.orderVenueId;
      postOrderRefund(ruleForm).then(res => {
        if (res.code == 200) {
          ElMessage({
            type: 'success',
            message: res.msg,
          })
        }
        formEl.resetFields();
        dialogVisible.value = false;
      })
    }
  })
};

// 取消按钮
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dialogVisible.value = false;
};

defineExpose({
  dialogVisible,
});
</script>
<style lang="scss" scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
